package com.yan.edu.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.injector.ISqlInjector;

import com.baomidou.mybatisplus.extension.injector.LogicSqlInjector;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yan.commonutils.R;
import com.yan.edu.entity.Teacher;
import com.yan.edu.entity.vo.TeacherQuery;
import com.yan.edu.service.TeacherService;
import com.yan.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-09-08
 */
@Api(tags="讲师管理")
@RestController
@CrossOrigin
@RequestMapping("/edu/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;
    @ApiOperation(value = "所有讲师列表")
    @GetMapping("findAll")
    public R findAllTeacher(){
        List<Teacher> list = teacherService.list(null);
        return R.ok().data("items",list);
    }

//讲师逻辑删除
@ApiOperation(value = "根据ID删除讲师")
    @DeleteMapping("{id}")
    public R removeById( @ApiParam(name = "id", value = "讲师ID", required = true)@PathVariable String id){
    boolean b = teacherService.removeById(id);
    if(b){
        return R.ok();
    }else{
        return R.error();
    }
}

@GetMapping("/pageTeacher/{current}/{limit}")
    public R pageListTeacher(
        @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable long current,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable long limit){
    try {
        int i = 10/0;
    }catch(Exception e) {
        throw new GuliException(20001,"出现自定义异常");
    }

    Page<Teacher> pageTeacher = new Page<>(current, limit);

    teacherService.page(pageTeacher,null);
    List<Teacher> records = pageTeacher.getRecords();
    long total = pageTeacher.getTotal();
    return  R.ok().data("total", total).data("rows", records);



}
//条件查询代分页
@PostMapping("pageTeacherCondition/{current}/{limit}")
    public R pageTeacherCondition(@PathVariable long current, @PathVariable long limit,
                                  @RequestBody(required = false) TeacherQuery teacherQuery){
//        创建page对象
    Page<Teacher> pageTeacher = new Page<>(current, limit);

//构建条件
    QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
//    多条件组合查询
//    动态SQL
//    判断条件值是否为空，如果不为空则拼接条件
    String name=teacherQuery.getName();
    Integer level=teacherQuery.getLevel();
    String begin=teacherQuery.getBegin();
    String end=teacherQuery.getEnd();
    if(!StringUtils.isEmpty(name)){
        wrapper.like("name",name);

    }
 if(!StringUtils.isEmpty(level)){
        wrapper.eq("level",level);

    }
 if(!StringUtils.isEmpty(begin)){
        wrapper.ge("gmt_create",begin);

    }
 if(!StringUtils.isEmpty(end)){
        wrapper.le("gmt_modified",end);

    }
// 排序
    wrapper.orderByDesc("gmt_create");

//    调用方法来实现分页查
    teacherService.page(pageTeacher,wrapper);
    List<Teacher> records = pageTeacher.getRecords();
    long total = pageTeacher.getTotal();
    return  R.ok().data("total", total).data("rows", records);


}
    @ApiOperation(value="添加讲师接口方法")
    //添加讲师接口的方法
    @PostMapping("addTeacher")
    public R addTeacher(@RequestBody Teacher teacher) {
        boolean save = teacherService.save(teacher);
        if(save) {
            return R.ok();
        } else {
            return R.error();
        }
    }
    @ApiOperation(value="根据id查询讲师")
    @GetMapping("getTeacher/{id}")
    public R getTeacher(@PathVariable String id){
        Teacher teacher=teacherService.getById(id);
        return R.ok().data("teacher",teacher);


    }
    @ApiOperation(value="根据id修改讲师")
    @PostMapping("updateTeacher")
    public R updateTeacher(@RequestBody Teacher teacher) {

        boolean flag = teacherService.updateById(teacher);
        if (flag) {
           return R.ok();
        } else {
            return R.error();
        }


    }

}

